package org.jeecg.modules.order.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.common.exception.IllegalPayOrderException;
import org.jeecg.modules.order.controller.OrderQuery;
import org.jeecg.modules.order.entity.Order;
import org.jeecg.modules.order.mapper.OrderMapper;
import org.jeecg.modules.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @Description: 订单
 * @Author: jeecg-boot
 * @Date: 2019-10-06
 * @Version: V1.0
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;

    public Order findOrderByPayOrderNo(String payOrderNo) {
        List<Order> list = orderMapper.findOrderByPayOrderNo(payOrderNo);
        if (list.isEmpty()) {
            throw new IllegalPayOrderException("Invalid payOrder!");
        } else {
            return list.get(0);
        }
    }

    @Override
    public Order findOrderByOrderNo(String orderNo) {
        List<Order> list = orderMapper.findOrderByOrderNo(orderNo);
        if (list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }

    @Override
    public Order getById(String id) {
        Order order = this.getById(id);
        DecimalFormat df = new DecimalFormat("0.00");
        Long amount = order.getAmount();
        if (amount != null) {
            BigDecimal bigDecimal = new BigDecimal(amount);
            BigDecimal divide = bigDecimal.divide(BigDecimal.valueOf(1000l));
            order.setAmountStr(df.format(divide));
        }
        return null;
    }

    @Override
    public IPage<Order> page(IPage<Order> page, OrderQuery orderQuery) {
        List<Order> list = orderMapper.page(page, orderQuery);
        page.setRecords(list);
        return page;
    }
}
